<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Account/classes/PhpFox_ComponentRandomImage.class.php, $Author:  $ */

App::loadClass('file/Image');

/** Random image (with digits)
 *
 * @package module.account
 * @version $Id: PhpFox_ComponentRandomImage.class.php 121 2007-12-21 22:38:19Z phpfoxsv $
 *
 */
class PhpFox_Mod_Account_ComponentRandomImage extends BaseComponent
{
    /** Processing method
     */
	function process()
	{
		@ob_clean();		
		$oDb =& Database::get();		
		$sStr = $this->_generateCode(6);
		
		$oLog =& App::getModuleService('Log', 'Log');
		
		file_Image::displayRandom($sStr, 120, 35, 10, 25);
		$iTime = time();
		
		$iCnt = $oDb->getField("SELECT COUNT(*) FROM " . App::getT('captcha') . " WHERE captcha_id = '" . $oLog->getUserSession() . "'");
		if ($iCnt) 
		{
			$oDb->update(App::getT('captcha'), array('imagehash' => md5(md5($sStr) . $oLog->getUserSession()), 'time' => $iTime), "captcha_id = '" . $oLog->getUserSession() . "'");	
		}
		else 
		{
			$oDb->insert(App::getT('captcha'), array('captcha_id' => $oLog->getUserSession(), 'imagehash' => md5(md5($sStr) . $oLog->getUserSession()), 'time' => $iTime));	
		}
		exit;
	}
	
   function _generateCode($characters) 
   {   
      $possible = '23456789bcdfghjkmnpqrstvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ';
      $code = '';
      $i = 0;
      while ($i < $characters) 
      { 
         $code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
         $i++;
      }      
      return $code;
   }	
	
}
?>